import {
  Cell,
  CELL
} from "./cell"
import Cloak from "./cloak"
import {
  Img,
  ImgType
} from "./img"

const FLELD = {

}

class Fleld {
  // 初始化
  constructor(game) {
    this.game = game

    this.cells = []
    this.flags = []
    this.firstMine = true
  }
  // 场景重置
  reset() {
    this.cells = []
    this.flags = []
    this.firstMine = true
  }
  // 创建场景
  createField() {
    for (let row = 0; row < this.game.level.row; row++) {
      this.cells.push([])
      for (let col = 0; col < this.game.level.col; col++) {
        let cell = new Cloak(row, col)
        this.cells[row].push(cell)
      }
    }
  }
  // 开局开一块区域 梦幻开局
  initOpen() {
    let r = Math.round(Math.random() * (this.game.level.row - 1 - 3))
    let c = Math.round(Math.random() * (this.game.level.col - 1 - 4))
    for (let row = r; row < r + 3; row++) {
      for (let col = c; col < c + 4; col++) {
        let cell = this.cells[row][col]
        if (cell.isMine) {
					if (this.game.Vue.auto){
						this.addFlag({
            evt: {
              button: 2
            }
          }, cell)
					}

        } else {
          this.showArea({
            evt: 0,
            type: 'tap'
          }, cell)
        }
      }
    }
  }
  // 立flag
  addFlag(e, cell) {
    if (e.evt.button != 2) return
    if (cell.flag) return
    if (this.game.status != 0) return
    if (this.game.flags == 0) return
    if (!cell.isCloak) return
    cell.setFlag()
    this.flags.push(cell)
    this.game.flags = this.game.level.mines - this.flags.length
  }
  // 移除flag
  removeFlag(cell) {
    if (this.game.status != 0) return
    for (let i = 0; i < this.flags.length; i++) {
      if (cell.flag.id == this.flags[i].flag.id) {
        this.flags.splice(i, 1)
        break
      }
    }
    cell.flag = null
    this.game.flags = this.game.level.mines - this.flags.length
  }
  // 翻开一块格子
  showCell(cell) {
    if (cell.show(true)) {
      return this.game.gameover()
    }
    if (cell.flag) this_.removeFlag(cell) // 是旗子就解除
    this.game.cloaks--
  }
  // 翻开一片区域
  showArea(e, cell) {
    if (!(e.evt.button == 0 || e.type == 'tap')) return
    if (cell.flag) return this.removeFlag(cell)

    function appear(cells, this_) {
      for (let cell of cells) {
        // 遮盖住的不是雷 就翻开
        if (cell.isCloak && !cell.isMine) {
          if (cell.value == null) {
            // 空格子 递归
            this_.showCell(cell)
            appear(this_.searchCell8(cell), this_)
          } else {
            // 数字格子 附近有雷就不开
            let res = this_.searchCell8(cell),
              sign = true
            for (let c of res) {
              if (cell.isCloak && c.isMine) {
                sign = false
                break
              }
            }
            this_.showCell(cell)
          }
        }
      }
    }
    if (this.game.status == -1) {
      // 未开始阶段 用户点击格子后游戏开始计时
      this.game.startTime()
      this.game.status = 0 // 变为游戏进行状态
    }
    if (this.game.status != 0) return

    if (!cell.isCloak) return this.autoFind(cell)
    this.showCell(cell)

    let cells = this.searchCell8(cell) // 开4个格子
    // 是空格子就递归开
    if (cell.value == null) appear(cells, this)
    this.isWin()

    if (this.game.Vue.auto) this.autoFlag()
  }
  // 自动寻找 翻开了的话就 查找四周的标记看看
  autoFind(cell) {
    if (!cell.value) return
    let cells = this.searchCell8(cell)
    let flag = 0
    for (let c of cells) {
      if (c.isCloak) {
        if (c.flag) flag++
      }
    }
    if (cell.value == flag) {
      for (let c of cells) {
        // if (c.isCloak && !c.isMine) this.showArea({type:'tap',evt:0},c)
        if (c.isCloak && !c.flag) this.showArea({
          type: 'tap',
          evt: 0
        }, c)
      }
    }
  }
  // 自动插旗
  autoFlag() {
    for (let row = 0; row < this.game.level.row; row++) {
      for (let col = 0; col < this.game.level.col; col++) {
        let cell = this.cells[row][col]
        if (!cell.value || cell.isCloak) continue
        let cells = this.searchCell8(cell)
        let mine = 0
        for (let c of cells) {
          if (c.isCloak) {
            if (!c.isMine) {
              mine += 6;
              break
            }
            if (++mine > cell.value) {
              mine++
              break
            }
          }
        }
        if (mine != cell.value) continue
        for (let c of cells) {
          this.addFlag({
            evt: {
              button: 2
            }
          }, c)
        }
      }
    }
  }
  //判断胜利条件
  isWin() {
    // 剩下的格子和雷一样多
    if (this.game.cloaks == this.game.level.mines) {
      this.game.gamepass()
    }
  }
  // 失败 显示所有雷
  showMines() {
    for (let row = 0; row < this.game.level.row; row++) {
      for (let col = 0; col < this.game.level.col; col++) {
        let cell = this.cells[row][col]
        if (cell.isMine) cell.show()
      }
    }
  }

  // 生成雷区
  createMines() {
    let maxCell = this.game.level.row * this.game.level.col
    let mines = new Set()
    while (mines.size < this.game.level.mines) {
      let r = Math.round(Math.random() * (maxCell - 1))
      mines.add(r)
      if (mines.size == maxCell) break
    }
    mines = Array.from(mines)
    for (let mine of mines) {
      let row = mine % this.game.level.row
      let col = Math.trunc(mine / this.game.level.row)
      this.cells[row][col].setMine()
    }
  }
  // 生成数字
  createNumber() {
    // 判断边界 统计周围雷
    for (let row = 0; row < this.game.level.row; row++) {
      for (let col = 0; col < this.game.level.col; col++) {
        let cell = this.cells[row][col]
        // 不是雷 就往里加数字 或 空
        if (!cell.isMine) {
          let mine = 0
          let cells = this.searchCell8(cell)
          for (let c of cells)
            if (c.isMine) mine++
          cell.setValue(mine == 0 ? null : mine)
        }
      }
    }
  }
  // 搜索四周上下左右4个格子
  searchCell4(cell) {
    let maxRow = this.game.level.row,
      maxCol = this.game.level.col,
      res = [],
      l = [
        [-1, 0],
        [0, -1],
        [0, 1],
        [1, 0],
      ]
    for (let a of l) {
      let r = a[0] + cell.row
      let c = a[1] + cell.col
      if (r < 0 || c < 0 || r >= maxRow || c >= maxCol) continue
      res.push(this.cells[r][c])
    }
    return res
  }
  // 搜索四周8个格子
  searchCell8(cell) {
    let maxRow = this.game.level.row,
      maxCol = this.game.level.col,
      res = [],
      l = [
        [-1, -1],
        [-1, 0],
        [-1, 1],
        [0, -1],
        [0, 1],
        [1, -1],
        [1, 0],
        [1, 1]
      ]
    for (let a of l) {
      let r = a[0] + cell.row
      let c = a[1] + cell.col
      if (r < 0 || c < 0 || r >= maxRow || c >= maxCol) continue
      let ce = this.cells[r][c]
      res.push(ce)
    }
    return res
  }
}
export default Fleld
